﻿using System;
using System.Collections.Generic;
using System.Text;
using DotNet.Model;
using DotNet.Common;
using DotNet.Data.Factory;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
namespace DotNet.Data
{
	public class ProductTabDal
	{
		IDBHelp db = DBHelp.Create();
		 Hashtable htSyn = Hashtable.Synchronized(new Hashtable());
		StringBuilder strSql = new StringBuilder();

		#region 是否存在该记录
		/// <summary>
		/// 是否存在该记录
		/// </summary>
		/// <param name="TabID">标识列ID</param>
		public bool Exists(int TabID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select count(1) from [ProductTab] where TabID=@tabid ");
			htSyn.Add("@tabid", TabID);
			return db.IsExists(strSql.ToString(), htSyn);
		}
		#endregion

		#region 得到该表数据列表
		/// <summary>
		/// 得到该表数据列表
		/// </summary>
		public IList<ProductTab> GetModel()
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("Select [TabID],[ProductID],[TabSort],[TabContent],[TabTitle] FROM [ProductTab]");
			 return getProductTabBySql(strSql.ToString());
		}
		#endregion

		#region 根据标识列查询实体
		/// <summary>
		/// 根据标识列查询实体
		/// </summary>
		/// <param name="TabID">标识列ID</param>
		/// <returns></returns>
		public ProductTab GetModel(int tabid)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("select [TabID],[ProductID],[TabSort],[TabContent],[TabTitle] FROM [ProductTab]");
			strSql.Append(" where TabID=@tabid ");
            htSyn.Add("@tabid", tabid);
			DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
			ProductTab producttabModel = new ProductTab();
			if (dt.Rows.Count > 0)
			{
				producttabModel.productID=Convert.ToInt32(dt.Rows[0]["ProductID"]);
				producttabModel.tabSort=Convert.ToInt32(dt.Rows[0]["TabSort"]);
				producttabModel.tabContent=dt.Rows[0]["TabContent"].ToString();
				producttabModel.tabTitle=dt.Rows[0]["TabTitle"].ToString();
			}
			return producttabModel;
		}
		#endregion

        #region 根据产品ID查询TAB头部
        /// <summary>
        /// 根据产品ID查询TAB头部
        /// </summary>
        /// <param name="TabID">产品ID</param>
        /// <returns></returns>
        public IList<ProductTab> GetModelByProId(int pid)
        {
            htSyn.Clear();
            strSql.Remove(0, strSql.Length);
            strSql.Append("select [TabID],[ProductID],[TabSort],[TabTitle] FROM [ProductTab]");
            strSql.Append(" where ProductID=@ProductID order by [TabSort]");
            htSyn.Add("@ProductID", pid);
            IList<ProductTab> list = new List<ProductTab>();
            DataTable dt = db.GetDataTable(strSql.ToString(), htSyn);
            ProductTab producttabModel = new ProductTab();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    ProductTab pt = new ProductTab();
                    pt.tabID = Convert.ToInt32(dr["TabID"]);
                    pt.productID = Convert.ToInt32(dr["ProductID"]);
                    pt.tabSort = Convert.ToInt32(dr["TabSort"]);
                    pt.tabTitle = dr["TabTitle"].ToString();
                    list.Add(pt);
                }
            }
            return list;
        }
        #endregion
        

		#region 修改信息
		/// <summary>
		/// 修改信息
		/// </summary>
		/// <param name="ProductTab">实体</param>
		/// <returns></returns>
		public bool Update(ProductTab producttabModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("update [ProductTab] set ");
			strSql.Append("[ProductID]=@productid,");
			strSql.Append("[TabSort]=@tabsort,");
			strSql.Append("[TabContent]=@tabcontent,");
			strSql.Append("[TabTitle]=@tabtitle");
			strSql.Append(" where TabID=@tabid ");
			htSyn.Add("@tabid", producttabModel.tabID);
			htSyn.Add("@productid", producttabModel.productID);
			htSyn.Add("@tabsort", producttabModel.tabSort);
			htSyn.Add("@tabcontent", producttabModel.tabContent);
			htSyn.Add("@tabtitle", producttabModel.tabTitle);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 删除信息
		/// <summary>
		/// 删除信息
		/// </summary>
		/// <param name="TabID">标识列ID</param>
		/// <returns></returns>
		public bool Delete(int TabID)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("delete from [ProductTab] where TabID=@tabid");
			htSyn.Add("tabid", TabID);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 添加信息
		/// <summary>
		/// 添加信息
		/// </summary>
		/// <param name="ProductTab">实体</param>
		/// <returns></returns>
		public bool Insert(ProductTab producttabModel)
		{
			htSyn.Clear();
			strSql.Remove(0, strSql.Length);
			strSql.Append("insert into [ProductTab]([ProductID],[TabSort],[TabContent],[TabTitle]) values (@productid,@tabsort,@tabcontent,@tabtitle)");
			htSyn.Add("@productid", producttabModel.productID);
			htSyn.Add("@tabsort", producttabModel.tabSort);
			htSyn.Add("@tabcontent", producttabModel.tabContent);
			htSyn.Add("@tabtitle", producttabModel.tabTitle);
			if (db.ExecuteNonQuery(strSql.ToString(), htSyn) > 0)
			{
				return true;
			}
			return false;
		}
		#endregion

		#region 根据SQL语句获取集合
		/// <summary>
		///根据SQL语句获取集合
		/// </summary>
		/// <param name="strSql">sql语句</param>
		public IList<ProductTab> getProductTabBySql(string strSql)
		{
			IList<ProductTab> list = new List<ProductTab>();
			DataTable dt = db.GetDataTable(strSql);
			if (dt.Rows.Count > 0)
			{
				foreach (DataRow dr in dt.Rows)
				{
					ProductTab producttabModel = new ProductTab();
					producttabModel.tabID= Convert.ToInt32(dr["TabID"]);
					producttabModel.productID= Convert.ToInt32(dr["ProductID"]);
					producttabModel.tabSort= Convert.ToInt32(dr["TabSort"]);
					producttabModel.tabContent=dr["TabContent"].ToString();
					producttabModel.tabTitle=dr["TabTitle"].ToString();
					list.Add(producttabModel);
				}
			}
			return list;
		}
		#endregion
	}
}
